/*
 * PlaneControl.cpp
 *
 *  Created on: 2012/07/12
 *      Author: weihe
 */

#include <ctime>
#include "PlaneControl.h"
#include <string>
#include <iostream>
#include <sstream>
#include <gl/glut.h>

using namespace std;

namespace object {

PlaneControl::~PlaneControl()
{
	delete scoreTxt;
	delete timeTxt;
	delete fpsTxt;
	delete aiming;
}

bool PlaneControl::update(float dt)
{
	stringstream scoreTxtSS(stringstream::in | stringstream::out);
	scoreTxtSS << "Score: " << score;
	scoreTxt->txt = scoreTxtSS.str();

	stringstream timeTxtSS(stringstream::in | stringstream::out);
	int leftTime = deadTime - clock();
	if (leftTime < 0) {
		gameOver = true;
	}
	timeTxtSS << "Time: " << leftTime / CLOCKS_PER_SEC;
	timeTxt->txt = timeTxtSS.str();

	stringstream fpsSS(stringstream::in | stringstream::out);
	fpsSS << "FPS: " << (int) (1.0f / dt);
	fpsTxt->txt = fpsSS.str();
	return true;
}

void PlaneControl::draw()
{
	glDisable(GL_LIGHTING);

	glPushAttrib(GL_ENABLE_BIT);
	{
		glMatrixMode(GL_PROJECTION);
		glPushMatrix();
		{
			glLoadIdentity();
			gluOrtho2D(0, window.width, window.height, 0);
			glMatrixMode(GL_MODELVIEW);
			glPushMatrix();
			{
				glLoadIdentity();
				if (!gameOver) {
					glColor3f(0.0, 0.0, 1.0);
					fpsTxt->draw();
					scoreTxt->draw();
					timeTxt->draw();
					aiming->draw();
				} else {
					Text overTxt(window.center().x - 10, window.center().y - 4,
							GLUT_BITMAP_TIMES_ROMAN_24);
					stringstream ss(stringstream::in | stringstream::out);
					ss << "Time up!\nScore: " << score;
					overTxt.txt = ss.str();

					glColor3f(0, 0, 1);
					overTxt.draw();
					glutIdleFunc(NULL);
				}
			}
			glPopMatrix();
			glMatrixMode(GL_PROJECTION);
		}
		glPopMatrix();
	}
	glPopAttrib();
	glMatrixMode(GL_MODELVIEW);

	glEnable(GL_LIGHTING);

}

} /* namespace object */
